package com.health.memberbackstage.web;

import com.health.memberbackstage.pojo.Checkgroup;
import com.health.memberbackstage.pojo.R.Result;
import com.health.memberbackstage.pojo.Setmeal;
import com.health.memberbackstage.pojo.SetmealDetails;
import com.health.memberbackstage.service.SetmealDetailsService;
import com.health.memberbackstage.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/details")
public class SetmealDetailsController {

    @Autowired
    private SetmealDetailsService setmealDetailsService;

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/{id}")
    private Result findAll(@PathVariable("id") Long id){
        SetmealDetails setmealDetails = new SetmealDetails();

        Setmeal setmeal = setmealService.getById(id);

        int count = setmealDetailsService.findSetmealInItemCount(id);

        List<Checkgroup> checkgroupNames = setmealDetailsService.findCgNamesBySetmealID(id);


        List<Checkgroup> checkgroup = setmealDetailsService.findCgNamesBySetmealID(id);

        ArrayList<Object> list = new ArrayList<>();
        for(Checkgroup c : checkgroup) {
            Integer ciId = c.getId();
            long ciID = Long.parseLong(ciId.toString());
            List<String> ciNamesBySetmealID = setmealDetailsService.findCINamesBySetmealID(ciID);
            list.add(ciNamesBySetmealID);
        }


        List<String> checkgroupRemarks = setmealDetailsService.findCgRemarksBySetmealID(id);

        setmealDetails.setSetmeal(setmeal);
        setmealDetails.setSetmealCount(count);
        setmealDetails.setCheckgroupNames(checkgroupNames);
        setmealDetails.setCheckitemNames(list);
        setmealDetails.setCheckgroupRemarks(checkgroupRemarks);

        if (setmealDetails != null){
            return Result.success(setmealDetails);
        }
        return Result.fail("查询失败");
    }
}
